package org.example.myleet.rosalind.orf;

import org.example.myleet.rosalind.utils.DnaCodonReader;
import org.example.myleet.rosalind.utils.DnaReverseComplementor;
import org.example.myleet.rosalind.utils.FastaReader;
import org.example.myleet.rosalind.utils.FastaSequence;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Solution {

    private static final Map<String, String> DNA_CODON_MAP_AMINO_ACID = DnaCodonReader.getDnaCodonMapAminoAcid();

    private Set<String> proteinSet = new HashSet<>();

    public void findAllOrfForDoubleStrandDna(String input) {
        List<FastaSequence> dnaSequences = FastaReader.parseFastaInput(input);
        String dna = dnaSequences.get(0).getSequence();
        findAllOrfOnOneStrandDna(dna);
        findAllOrfOnOneStrandDna(DnaReverseComplementor.reverseComplete(dna));
    }

    public void findAllOrfOnOneStrandDna(String dna) {
//        System.out.printf("Input DNA: %s%n", dna);
        for (int i = 0; i < dna.length() - 5; ++i) {
            if (dna.charAt(i) == 'A' && dna.charAt(i + 1) == 'T' && dna.charAt(i + 2) == 'G') {
                int j = i;
                boolean validOrf = false;
                String codon;
                StringBuilder protein = new StringBuilder();
                while (j < dna.length() - 2) {
                    codon = dna.substring(j, j + 3);
                    String aminoAcid = DNA_CODON_MAP_AMINO_ACID.get(codon);
                    if ("Stop".equals(aminoAcid)) {
                        validOrf = true;
                        break;
                    }
                    protein.append(aminoAcid);
                    j += 3;
                }
                if (validOrf && proteinSet.add(protein.toString())) {
                    System.out.println(protein);
                }
            }
        }
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        solution.findAllOrfForDoubleStrandDna(">Rosalind_24\nAGCCATGTAGCTAACTCAGGTTACATGGGGATGACCCCGCGACTTGGATTAGAGTCTCTTTTGGAATAAGCCTGAATGATCCGAGTAGCATCTCAG\n");
        System.out.println("--------------------------");
        solution.findAllOrfForDoubleStrandDna("G>Rosalind_7443\n" +
                "GAATACCACTCCAATATAGTCCACACAAGTCCCGTATCAACTACGCTCGTCGAACAGGCA\n" +
                "AACGTAGTGAGTTCCGCACTAACTCCCGTTCGGGACGGTGAATTCACCGAATAAGACACT\n" +
                "ACCCGACATATACCCCTCAACTGATGAGCCCAGTGAGGCTTTCGTAACGCGCATCTAGAA\n" +
                "CACACAGGACAACACACTTGTACACGGGTGGGCTTGAGCTAAGCGTTCCGGTCTGTTGCT\n" +
                "GGAGGCCGGAAAGCAGGTTTTTTGTAGTCTGCTAAGTGACACTCGGTGAGAATACGCTCA\n" +
                "GTGGAGTATGTTCAACATGGTCGACCATTTTCAGTTGGGCAGACAAGCAGTTTTGACCAA\n" +
                "GCGTTTAAGCCGTAGAAGTTAACGATTCGATTTAGCGCAAAACATCTTCACATCGTTCCC\n" +
                "CCTATATACTGACATCACAGAATGACGGTAATGATTGTGGTTAATGCCCTCTAGCTAGAG\n" +
                "GGCATTAACCACAATCATTACCGTCATAGTGACTATGTTGATCTAGTGTATATGTAGATG\n" +
                "CAAAGACGCATTTACTGGCGCCGAAATGAAGTTTACGGGCCTATAAGTAACCGGGGTCTT\n" +
                "ACCGCTGCACTCCTGGTATCGATATCCGGGTACATGGCTAGAGAAGGGCTAGTTCAACGT\n" +
                "TTGAACTGGCTGCCATAATAGTCTTACTCATGCTACGCTCTGCTTCCCATAGATCTGCGC\n" +
                "GCAACGTATACTTGATCGCACTCAGCGTGGCCTTCTTACGGTGACCTGCATTCTGCGTTG\n" +
                "CATGGACTCATTGTTTGGGTTTCGCAACTCAGTACGGAGCGAAGCTATCCTTTCTGAGGG\n" +
                "TCACGGGGTCCTGCGTCGATGGACCCTTCGTCCTCTCTGCAACATAAAAGGCGGTGTCAC\n" +
                "TCTTGTACAGAAGCCTTCACGTGGGTGCACAGACACCTGGCGTAATCC\n");
    }
}
